package com.example.admin.rxjavademo;

import android.database.Observable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        demo1();
    }

    /**
     * 联系基本的被观察者和观察者以及订阅
     */
    private void demo1() {
        io.reactivex.Observable.create(new ObservableOnSubscribe<Integer>() {
            /**
             * Called for each Observer that subscribes.
             *
             * @param e the safe emitter instance, never null
             * @throws Exception on error
             */
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
                Log.e(TAG, "subscribe: " + 1 );
                e.onNext(1);
                Log.e(TAG, "subscribe: " + 2 );
                e.onNext(2);
                Log.e(TAG, "subscribe: " + 3 );
                e.onNext(3);
                Log.e(TAG, "subscribe: " + 4 );
                e.onNext(4);
                Log.e(TAG, "subscribe: " + "complete" );
                e.onComplete();
            }
        }).subscribe(
                new Observer<Integer>() {
                    Disposable mDisposable;
                    private static final String TAG = "Observer";
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                        Log.e(TAG, "onSubscribe: " );
                    }

                    @Override
                    public void onNext(@NonNull Integer integer) {
                        Log.e(TAG, "onNext: " + integer );
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: " );
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: " );
                    }
                }
        );
    }
}
